package com.red.star.wechat.work.site.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.red.star.wechat.data.entity.Mall;
import com.red.star.wechat.data.mappers.MallMapper;
import com.red.star.wechat.data.utils.CheckUtil;
import com.red.star.wechat.work.constant.Constant;
import com.red.star.wechat.work.entity.Admin;
import com.red.star.wechat.work.entity.Role;
import com.red.star.wechat.work.entity.TableContainer;
import com.red.star.wechat.work.site.admin.mapper.AdminMapper;
import com.red.star.wechat.work.site.admin.mapper.RoleMapper;
import com.red.star.wechat.work.utils.CommonUtil;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xulonglong on 2017/6/15.
 */
@Service
public class AdminService {

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private MallMapper mallMapper;

    @Resource
    private RoleMapper roleMapper;

    public Admin findAdminById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    public Admin findAdminByUsername(String username) {
        return adminMapper.findAdminByUsername(username);
    }

    public List<Admin> findAll() {
        return adminMapper.findAll();
    }

    public TableContainer findAll(Admin admin) {
        Page<?> page = PageHelper.startPage(admin.getPage(), admin.getRows());
        List<Admin> adminList = adminMapper.selectAll();
        if (!CheckUtil.isEmpty(adminList)) {
            for (Admin child : adminList) {
                Example example = new Example(Mall.class);
                Example.Criteria criteria = example.createCriteria();
                if (Constant.B_REGION_CODE.equalsIgnoreCase(child.getEnumValue())) {//大区
                    criteria.andEqualTo("bRegionCode", child.getBRegionCode());
                    List<Mall> mallList = mallMapper.selectByExample(example);
                    if (!CheckUtil.isEmpty(mallList)) {
                        Mall mall = mallList.get(0);
                        child.setBRegionName(mall.getBRegionName());
                    }
                } else if (Constant.S_REGION_CODE.equalsIgnoreCase(child.getEnumValue())) {//小区
                    criteria.andEqualTo("bRegionCode", child.getBRegionCode());
                    if (!CheckUtil.isEmpty(child.getSRegionCode())) {
                        criteria.andEqualTo("sRegionCode", child.getSRegionCode());
                    }
                    List<Mall> mallList = mallMapper.selectByExample(example);
                    if (!CheckUtil.isEmpty(mallList)) {
                        Mall mall = mallList.get(0);
                        child.setBRegionName(mall.getBRegionName());
                        child.setSRegionName(mall.getSRegionName());
                    }
                } else if (Constant.MALL_CODE.equalsIgnoreCase(child.getEnumValue())) {//商场
                    criteria.andEqualTo("bRegionCode", child.getBRegionCode());
                    if (!CheckUtil.isEmpty(child.getSRegionCode())) {
                        criteria.andEqualTo("sRegionCode", child.getSRegionCode());
                    }
                    criteria.andEqualTo("mallCode", child.getMallCode());
                    List<Mall> mallList = mallMapper.selectByExample(example);
                    if (!CheckUtil.isEmpty(mallList)) {
                        Mall mall = mallList.get(0);
                        child.setBRegionName(mall.getBRegionName());
                        child.setSRegionName(mall.getSRegionName());
                        child.setMallName(mall.getMallName());
                    }
                } else if (Constant.MERCHANT_ID.equalsIgnoreCase(child.getEnumValue())) {//商户
                    criteria.andEqualTo("bRegionCode", child.getBRegionCode());
                    if (!CheckUtil.isEmpty(child.getSRegionCode())) {
                        criteria.andEqualTo("sRegionCode", child.getSRegionCode());
                    }
                    criteria.andEqualTo("mallCode", child.getMallCode());
                    List<Mall> mallList = mallMapper.selectByExample(example);
                    if (!CheckUtil.isEmpty(mallList)) {
                        Mall mall = mallList.get(0);
                        child.setBRegionName(mall.getBRegionName());
                        child.setSRegionName(mall.getSRegionName());
                        child.setMallName(mall.getMallName());
                    }
                }
            }
        }
        return CommonUtil.backTableContainer(adminList, page);
    }

    public List<Mall> findAllBRegionCodeAndBRegionName() {

        return mallMapper.findAllBRegionCodeAndBRegionName();
    }

    public List<Mall> findAllSRegionCodeAndSRegionName() {
        return mallMapper.findAllSRegionCodeAndSRegionName();
    }

    public List<Role> queryRole() {
        return roleMapper.selectAll();
    }

    public List<Mall> findAllMallName() {
        return mallMapper.findAllMallName();
    }


    public List<Mall> findAllSmArea(String bRegionCode) {
        return mallMapper.findAllSmArea(bRegionCode);
    }

    public List<Mall> findAllMallNameArea(String sRegionCode) {
        return mallMapper.findAllMallNameArea(sRegionCode);
    }


    public Admin findAdminByEmployeeId(String employeeId) {
        return adminMapper.findAdminByEmployeeId(employeeId);
    }

    public void addAdmin(Admin admin) {
        admin.setCreateTime(new Date());
        adminMapper.insert(admin);
        addAdminAndRole(admin);
    }

    public void addAdminAndRole(Admin admin) {
        String[] split = admin.getRuleId().split(",");
        Integer id = admin.getId();
        for (String roleId : split) {
            adminMapper.addAdminAndRole(id, Integer.parseInt(roleId));
        }

    }

    public void updateAdmin(Admin admin) {
        adminMapper.updateAdmin(admin);

        adminMapper.deleteAdminAndRole(admin.getId());

        addAdminAndRole(admin);

    }

    public Admin showUpdat(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    public void deleteAdminById(String id) {
        adminMapper.deleteAdminById(Integer.parseInt(id));
        adminMapper.deleteAdminAndRole(Integer.parseInt(id));
    }

    public List<Admin> queryAllAdminByEnumValue() {
//		List<Admin> adminList = adminMapper.queryAllAdminByEnumValue();
        List<Admin> adminList = new ArrayList<>();
        Admin admin = new Admin();
        admin.setEnumValue(Constant.REDSTART_MANAG);
        Admin admin1 = new Admin();
        admin1.setEnumValue(Constant.B_REGION_CODE);
        Admin admin2 = new Admin();
        admin2.setEnumValue(Constant.S_REGION_CODE);
        Admin admin3 = new Admin();
        admin3.setEnumValue(Constant.MALL_CODE);
        Admin admin4 = new Admin();
        admin4.setEnumValue(Constant.MERCHANT_ID);
        adminList.add(admin);
        adminList.add(admin1);
        adminList.add(admin2);
        adminList.add(admin3);
        adminList.add(admin4);
        return adminList;
    }

    public List<Mall> findBigArea(String mallCode) {
        return mallMapper.findBigArea(mallCode);
    }
}
